<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="英文名" prop="english">
      <el-input v-model="dataForm.english" placeholder="英文名"></el-input>
    </el-form-item>   
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="身高" prop="height">
      <el-input v-model="dataForm.height" placeholder="身高"></el-input>
    </el-form-item>
    <el-form-item label="臂展" prop="wingspan">
      <el-input v-model="dataForm.wingspan" placeholder="臂展"></el-input>
    </el-form-item>
    <el-form-item label="国家" prop="country">
      <el-input v-model="dataForm.country" placeholder="国家"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="birth">
      <el-input v-model="dataForm.birth" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="选秀" prop="draft">
      <el-input v-model="dataForm.draft" placeholder="选秀"></el-input>
    </el-form-item>
    <el-form-item label="薪资" prop="salary">
      <el-input v-model="dataForm.salary" placeholder="薪资"></el-input>
    </el-form-item>
    <el-form-item label="球队" prop="team">
      <el-input v-model="dataForm.team" placeholder="球队"></el-input>
    </el-form-item>
      <el-form-item label="号码" prop="team">
      <el-input v-model="dataForm.number" placeholder="号码"></el-input>
    </el-form-item>
    <el-form-item label="体重" prop="weight">
      <el-input v-model="dataForm.weight" placeholder="体重"></el-input>
    </el-form-item>
    <el-form-item label="位置" prop="position">
      <el-input v-model="dataForm.position" placeholder="位置"></el-input>
    </el-form-item>
    <el-form-item label="照片" prop="cover">
      <!-- <el-input v-model="dataForm.cover" placeholder="照片"></el-input> -->
      <single-upload v-model="dataForm.cover" :floder="this.floder"></single-upload>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from "@/components/upload/singleUpload"
  export default {
    components:{SingleUpload},
    data () {
      return {
        visible: false,
        floder:'player',
        dataForm: {
          id: 0,
          name: '',
          height: '',
          wingspan: '',
          country: '',
          birth: '',
          draft: '',
          salary: '',
          team: '',
          number:'',
          weight: '',
          position: '',
          cover: '',
          english: ''
        },
        dataRule: {
          name: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          english: [
            { required: true, message: '英文名不能为空', trigger: 'blur' }
          ],
          height: [
            { required: true, message: '身高不能为空', trigger: 'blur' }
          ],
          wingspan: [
            { required: true, message: '臂展不能为空', trigger: 'blur' }
          ],
          country: [
            { required: true, message: '国家不能为空', trigger: 'blur' }
          ],
          birth: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          draft: [
            { required: true, message: '选秀不能为空', trigger: 'blur' }
          ],
          salary: [
            { required: true, message: '薪资不能为空', trigger: 'blur' }
          ],
          team: [
            { required: true, message: '球队id不能为空', trigger: 'blur' }
          ],
           number: [
            { required: true, message: '球队号码不能为空', trigger: 'blur' }
          ],
          weight: [
            { required: true, message: '体重不能为空', trigger: 'blur' }
          ],
          position: [
            { required: true, message: '位置不能为空', trigger: 'blur' }
          ],
          cover: [
            { required: true, message: '照片不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/player/playerinformation/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.playerInformation.name
                this.dataForm.english = data.playerInformation.english
                this.dataForm.height = data.playerInformation.height
                this.dataForm.wingspan = data.playerInformation.wingspan
                this.dataForm.country = data.playerInformation.country
                this.dataForm.birth = data.playerInformation.birth
                this.dataForm.draft = data.playerInformation.draft
                this.dataForm.salary = data.playerInformation.salary
                this.dataForm.team = data.playerInformation.team
                this.dataForm.number = data.playerInformation.number
                this.dataForm.weight = data.playerInformation.weight
                this.dataForm.position = data.playerInformation.position
                this.dataForm.cover = data.playerInformation.cover
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/player/playerinformation/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'english': this.dataForm.english,
                'height': this.dataForm.height,
                'wingspan': this.dataForm.wingspan,
                'country': this.dataForm.country,
                'birth': this.dataForm.birth,
                'draft': this.dataForm.draft,
                'salary': this.dataForm.salary,
                'team': this.dataForm.team,
                'number': this.dataForm.number,
                'weight': this.dataForm.weight,
                'position': this.dataForm.position,
                'cover': this.dataForm.cover
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
